package com.xijia.wuyouwallet.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.ResidualAmountData;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/11 0011.
 */

public class MeWalletActivity extends BaseActivity implements View.OnClickListener{

    private String money;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_wallet);
        initView();
    }
    private ImageView me_wallet_back;

    private TextView me_wallet_crash,me_wallet_money,me_wallet_config;

    private void initView(){
        me_wallet_back = (ImageView) findViewById(R.id.me_wallet_back);
        me_wallet_back.setOnClickListener(this);
        me_wallet_crash = (TextView) findViewById(R.id.me_wallet_crash);
        me_wallet_crash.setOnClickListener(this);
        me_wallet_money = (TextView) findViewById(R.id.me_wallet_money);
        me_wallet_config = (TextView) findViewById(R.id.me_wallet_config);
        me_wallet_config.setOnClickListener(this);
        //initData();
        requestData();
    }

    /*private void initData(){
        me_wallet_money.setText(MyApp.userData.getData().getLeft_money());
    }*/

    private void requestData(){
        Map<String,Object> map = new HashMap<>();
        map.put("token",MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        XUtil.Post(UrlConfig.GET_RESIDUAL_AMOUNT,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("获取余额result="+result);
                ResidualAmountData data = JSONObject.parseObject(result,ResidualAmountData.class);
                if (data.getCode()==200){
                    money = data.getData().getAmount();
                    me_wallet_money.setText("￥"+money);
                }else {
                    me_wallet_money.setText("￥0.00");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取余额onError="+ex.toString());
            }
        });
    }

    @Override
    public void onClick(View view) {
        Bundle bundle = null;
        switch (view.getId()){
            case R.id.me_wallet_back:
                finish();
                break;
            case R.id.me_wallet_crash:
                bundle = new Bundle();
                bundle.putString("url", UrlConfig.CASH_LIST_URL+"/id/"+MyApp.userData.getData().getUser_id());
                jumpToPage(CommonWvActivity.class,bundle);
                break;
            case R.id.me_wallet_config:
                bundle = new Bundle();
                bundle.putString("money",money);
                jumpToPage(CrashActivity.class,bundle,true, Constants.REQUEST_DATA,false);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constants.REQUEST_DATA&&resultCode==Constants.RESULT_DATA){
            requestData();
        }
    }
}
